<?php
/*
 * Created on 21 janv. 2009
 * Created by Ludovic Reenaers
 * lreenaers@hotmail.com
 */
class MenuTreeGenerator extends Generator{
	private $lvl=0;
	function dump(){
		$DAO = $this->Factory->getCategorieDAO();
		$Bean = $DAO->getById(1);
		return $this->dumpCategory($Bean);

	}
	private function authorizeCategory($Bean){
		$bool = false;
		$tokens = $_SESSION['USER_BEAN']->getProfile()->getTokens();
		$forbidden = "";
		foreach ($tokens as $Token) {
			if(strtolower($Token->getLabel()) == "category#show"){
				$forbidden = explode(".", $Token->getXcept());
			}
		}
		if($forbidden=="" || !in_array((string)$Bean->getId(), $forbidden)){
			$bool = true;
		}
		return $bool;
	}
	private function dumpCategory($category){
		$strr=null;
		$this->lvl+=1;
		if(!$category->isRoot() && $this->authorizeCategory($category)){
				
				
			$strr="<ul>";
			$label = $category->getLabel($_SESSION['langue']);
			$tooltip = $category->getTooltip($_SESSION['langue']);
				
			if (((int)$_SESSION["catid"] == $category->getId() && strtolower($_SESSION["module"]=="category")) || ($category->containsDocument((int)$_SESSION["docid"]) && strtolower($_SESSION["module"]=="document"))) {

				$strr.='<li><span class="current">'.$label.'</span>';
			}else{

				$strr.='<li><a href="'.$_SESSION['config']->controler.'?/category/show/'.$category->getId().'/'.$label.'/" title="'.$tooltip.'" alt="'.$tooltip.'">'.$label.'</a>';
			}
			if(($category->hasChildCategorie((int)$_SESSION["catid"]) && strtolower($_SESSION["module"]=="category"))|| ($category->hasChildDocument((int)$_SESSION["docid"]) && strtolower($_SESSION["module"]=="document"))){
				foreach ($category->getChildCategories() as $cat){
					$strr.=$this->dumpCategory($cat);
				}
			}
			$strr.="</li></ul>";
		}else{
				
			if(($category->hasChildCategorie((int)$_SESSION["catid"]) && strtolower($_SESSION["module"]=="category"))|| ($category->hasChildDocument((int)$_SESSION["docid"]) && strtolower($_SESSION["module"]=="document"))||$this->lvl == 1){
				foreach ($category->getChildCategories() as $cat){
					$strr.=$this->dumpCategory($cat);
				}
			}
		}


		return $strr;
	}

}
?>
